package com.sihai.ehr.service.impl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.QueryCreator;
import com.sihai.ehr.model.biz.ExamPeriodAuditing;
import com.sihai.ehr.service.ExamPeriodAuditingService;

/**
 * 考核周期服务
 * 
 * @author Henry
 * 
 */
@Service("examPeriodAuditingService")
public class ExamPeriodAuditingServiceImpl implements ExamPeriodAuditingService {

	@Resource
	DataAccessor dataAccessor;
	
	@Override
	public ExamPeriodAuditing getExamPeriodAuditing(final Long periodId,final Long dutyId) {
		if (periodId == null || periodId < 0 || dutyId == null || dutyId < 0 ) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findUnique(ExamPeriodAuditing.class, new QueryCreator() {
				
				@Override
				public Query createQuery(Session session) {
					Query query = session
							.createQuery("from ExamPeriodAuditing e where e.examPeriod.id = :periodId and e.duty.id = :dutyId");
					query.setParameter("periodId", periodId);
					query.setParameter("dutyId", dutyId);
					return query.setFirstResult(0).setMaxResults(1);
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取考核周期考核对象数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：ExamPeriodAuditingServiceImpl.getExamPeriodAuditing;参数periodId=" + periodId
					+ ",dutyId=" + dutyId + "】");
		}
	}
	
	//@Override
	public ExamPeriodAuditing getExamPeriodAuditingByUserId(final Long periodId,final Long userId) {
		if (periodId == null || periodId < 0 || userId == null || userId < 0 ) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findUnique(ExamPeriodAuditing.class, new QueryCreator() {
				
				@Override
				public Query createQuery(Session session) {
					Query query = session
							.createQuery("from ExamPeriodAuditing e where e.examPeriod.id = :periodId and e.currentAuditerId = :userId");
					query.setParameter("periodId", periodId);
					query.setParameter("userId", userId);
					return query.setFirstResult(1).setMaxResults(1);
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取考核周期考核对象数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：ExamPeriodAuditingServiceImpl.getExamPeriodAuditingByUserId;参数periodId=" + periodId
					+ ",userId=" + userId + "】");
		}
	}
	
	@Override
	public void saveExamPeriodAuditing(ExamPeriodAuditing periodAuditing) {
		if (periodAuditing == null) {
			throw new RuntimeException("【传入参数非法】【periodAuditing为空】");
		}

		try {
			dataAccessor.saveOrUpdate(periodAuditing);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保存考核周期对象到数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：ExamPeriodAuditingServiceImpl.saveExamPeriodAuditing】");
		}
	}
	
	
	/*@Override
	public LimitedList<ExamPeriodAuditing> searchExamPeriodAuditings(final Long periodId, final Long dutyId) {
		if (periodId == null || dutyId == null || periodId < 0 || dutyId < 0) {
			throw new RuntimeException("【传入参数非法】【examinePeriodEnum为空】");
		}
		try {
			return dataAccessor.findByQuery(ExamPeriodAuditing.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery("from ExamPeriodAuditing e where 1<>2");
							dynamicQuery.appendIfNotNull(
									"and e.examPeriod.id =:periodId",
									"periodId", periodId);
							dynamicQuery.appendIfNotNull(
									"and e.duty.id =:dutyId",
									"dutyId", dutyId);

							return dynamicQuery.build(session);
						}
					},null,null);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核周期列表时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamPeriodServiceImpl.searchExamPeriodAuditings;参数periodId="
							+ periodId + "】");
		}
	}*/
	
	@Override
	public void removeExamPeriodAuditings(String periodAuditingIds) {
		// TODO Auto-generated method stub
		
	}

}
